package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Attention;
import cn.wolfcode.trip.base.domain.Great;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IAttentionService;
import cn.wolfcode.trip.base.service.IGreatService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/attention")
public class AttentionController {
    @Autowired
    private IAttentionService attentionService;
    //关注操作
    @PostMapping
    public Object great(Long userGetId){
        JSONResult jsonResult = new JSONResult();
            try {
                //首先查询记录是否存在该用户的记录
                //得到当前用户的id
                User user = UserContext.getCurrentUser();
                if (user==null){
                    jsonResult.setMsg("您未登录");
                    jsonResult.setSuccess(false);
                    return jsonResult;
                }
                Long userId = user.getId();
                Attention attention=attentionService.get(userId,userGetId);
                jsonResult.setData(1);
                //如果attention为!null就是取消关注操作,否则就是关注操作
                if (attention !=null){
                    //取消关注,删除点赞记录,同时被关注用户的关注数量减一
                    attentionService.deleteGreat(userId,userGetId);
                    attentionService.subGreatNum(userGetId);
                }else {
                    //增加一条关注记录
                    Attention attention1 = new Attention();
                    attention1.setUserId(user);
                    user = new User();
                    user.setId(userGetId);
                    attention1.setUserGetId(user);
                    //并且该用户的关注数量加一
                    attentionService.addGreatNum(userGetId);
                    attentionService.insertGreat(attention1);

                }
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    @GetMapping("/{userGetId}")
    public Object get(@PathVariable Long userGetId){//查看是否关注
        User user = UserContext.getCurrentUser();
        if(user != null){
            return  attentionService.get(user.getId(),userGetId);
        }
        return null;
    }
    @GetMapping
    public Object getuser(){
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user==null){
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    @GetMapping("/{id}/user")
    public Object userId(@PathVariable Long id){//查看关注者的信息
        List<User> users = attentionService.getUg_Id(id);
        return users;
    }
}
